package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Article;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.IArticleService;
import com.itheima.utils.JwtUtil;
import com.itheima.utils.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/article")
@RequiredArgsConstructor
@Validated
public class ArticleController {
    private final IArticleService articleService;
//登录验证测试
//    @GetMapping("/list")
//    public Result<String> list(/**@RequestHeader(name = "Authorization") String token, HttpServletResponse response**/){
////        if (token == null){
////            return Result.error("令牌为空");
////        }
////        try {
////            Map<String, Object> map = JwtUtil.parseToken(token);
////            return Result.success("文章列表...");
////        }catch (Exception e){
////            //set 404
////            response.setStatus(401);
////            return Result.error("未登录");
////        }
//        //添加过滤器统一过滤
//        return Result.success("文章列表...");
//    }
    @PostMapping
    public Result<String> addArticle(@RequestBody @Validated(Article.add.class) Article article){
        //获取用户，并设置时间
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        article.setCreateUser(userId);
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        articleService.save(article);
        return Result.success();

    }
    @GetMapping
    public Result<PageBean<Article>> pageQueryArticle(@RequestParam Integer pageNum
            ,@RequestParam Integer pageSize
            ,@RequestParam(required = false) Integer categoryId
            ,@RequestParam(required = false) String state){
        // 1.分页查询，new Page()的两个参数分别是：页码、每页大小
        Page<Article> p = Page.of(pageNum, pageSize);
        //设置wrapper条件
        LambdaQueryWrapper<Article> wrapper = new QueryWrapper<Article>().lambda()
                .eq(categoryId != null,Article::getCategoryId,categoryId)
                .eq(state != null,Article::getState,state);
        articleService.page(p,wrapper);
        List<Article> records = p.getRecords();
        //创建PagBean对象
        PageBean<Article> pageBean = new PageBean<>();
        pageBean.setTotal(p.getTotal());
        pageBean.setItems(records);
        return Result.success(pageBean);

    }
    @GetMapping("/detail")
    public Result<Article> articleDetailById(@RequestParam Integer id){
        Article article = articleService.getById(id);
        return Result.success(article);

    }
    @PutMapping
    public Result<String> updateArticle(@RequestBody Article article){
        //并设置更新时间
        article.setUpdateTime(LocalDateTime.now());
        articleService.updateById(article);
        return Result.success();

    }
    @DeleteMapping
    public Result<String> deleteArticleById(@RequestParam Integer id){
        articleService.removeById(id);
        return Result.success();

    }

}
